package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.utils.JwtUtils;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private  UserMapper userMapper;
    //查询用户(列表分页查询)
    @Override
    public PageResult<User> list(UserDto userDto) {
        //1.设置分页查询
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
        //2.调用mapper接口方法 执行查询
        List<User> userList  = userMapper.selectList(userDto);
        //3.将查询结果封装成PageResult对象并返回
        Page<User> p = (Page<User>)userList;
        return new PageResult<>(p.getTotal(),p.getResult());

    }
    //添加用户
    @Override
    public void add(User user) {
        //1.补全基础属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //2.补全默认密码
        //user.setPassword(user.getUsername()+"123");
        //此时在 数据库当中 密码是 明文显示的 需要改进
        //3.Md5加密
        String password = user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        //4.调用mapper接口方法 执行添加
        userMapper.insert(user);

    }

    @Override
    public void deleteUsers(List<Integer> ids) {
        userMapper.deleteUsersById(ids);

    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void updateUsers(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUsers(user);

    }
    //查询所有用户(直接展示)
    @Override
    public List<User> list() {
        return userMapper.list();
    }

    //根据角色查询用户
    @Override
    public List<User> listByRole(String roleLabel) {
        return userMapper.listByRole(roleLabel);
    }
    //根据部门查询用户信息
    @Override
    public List<User> listByDept(Integer deptId) {
        return userMapper.listByDept(deptId);
    }

    //登录
    @Override
    public LoginResultVo login(User user) {
        //调用mapper接口方法
        User u = userMapper.getByUsername(user.getUsername());
        //判断用户是否存在
        if(u == null){
            log.info("用户不存在");
            throw new BusinessException("用户名或密码错误");
        }
        //判断密码是否正确
          //首先对 前台 传过来得密码 进行md5加密
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
          //在与数据库当中的密码进行比较
        if(!u.getPassword().equals(password)){
            log.info("密码错误");
            throw new BusinessException("用户名或密码错误");
        }
        //判断用户状态status
        if(u.getStatus() != 1){
            log.info("用户被禁用");
            throw new BusinessException("对不起!您的用户已停用!!!");
        }
        //以上都正确得话,封装结果并返回
        LoginResultVo loginResult = new LoginResultVo();
        loginResult.setId(u.getId());
        loginResult.setUsername(u.getUsername());
        loginResult.setName(u.getName());
        loginResult.setImage(u.getImage());
        loginResult.setRoleLabel(u.getRoleLabel());
        //调用工具类生成token
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        loginResult.setToken(jwt);

        return loginResult;
    }
}
